package mx.ipn.persistencia.mysql;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;

import mx.ipn.persistencia.dao.RfcDAO;
import mx.ipn.to.RfcTO;

public class RfcDAOMySQL implements RfcDAO {
	static{FabricaDeDAOsMySQL.iniciaLog(RfcDAOMySQL.class);}

	private static final String INSERT = "INSERT INTO rfc (registro,denominacion) VALUES(?,?)";
	private static final String DELETE = "DELETE FROM rfc WHERE id_rfc=?";
	private static final String UPDATE = "UPDATE rfc SET registro=?,denominacion=? WHERE id_rfc=?";
	private static final String FINDBYID = "SELECT * FROM rfc WHERE id_rfc=?";
	private static final String FINDBYREGISTRO = "SELECT * FROM rfc WHERE registro=?";
	private static final String FINDBYDENOMINACION = "SELECT * FROM rfc WHERE denominacion=?";
	private static final String SELECT = "SELECT * FROM rfc";

	@Override
	public short deleteRfc(int idRfc) {
		try {
			Connection conexion = FabricaDeDAOsMySQL.crearConexion();

			PreparedStatement ps = conexion.prepareStatement(DELETE);

			ps.setInt(1, idRfc);

			ps.executeUpdate();
			if (ps.getUpdateCount() == 0) {
				ps.close();
				conexion.close();
				return 0;
			} else {
				ps.close();
				conexion.close();
				return 1;
			}
		} catch (Exception e) {
			e.printStackTrace();
			return 2;
		}
	}

	@Override
	public RfcTO findRfcByDenominacion(String denominacion) {
		try {
			Connection conexion = FabricaDeDAOsMySQL.crearConexion();

			PreparedStatement ps = conexion
					.prepareStatement(FINDBYDENOMINACION);

			ps.setString(1, denominacion);

			ResultSet rs = ps.executeQuery();

			if (rs.next()) {
				int idRfc = rs.getInt("id_rfc");
				String registro = rs.getString("registro");

				RfcTO rfcTO = new RfcTO(idRfc, registro, denominacion);
				ps.close();
				conexion.close();
				return rfcTO;
			}
			ps.close();
			conexion.close();
			return null;

		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}

	@Override
	public RfcTO findRfcById(int idRfc) {
		try {
			Connection conexion = FabricaDeDAOsMySQL.crearConexion();

			PreparedStatement ps = conexion.prepareStatement(FINDBYID);

			ps.setInt(1, idRfc);

			ResultSet rs = ps.executeQuery();

			if (rs.next()) {
				String registro = rs.getString("registro");
				String denominacion = rs.getString("denominacion");

				RfcTO rfcTO = new RfcTO(idRfc, registro, denominacion);
				ps.close();
				conexion.close();
				return rfcTO;
			}
			ps.close();
			conexion.close();
			return null;

		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}

	@Override
	public RfcTO findRfcByRegistro(String registro) {
		try {
			Connection conexion = FabricaDeDAOsMySQL.crearConexion();

			PreparedStatement ps = conexion.prepareStatement(FINDBYREGISTRO);

			ps.setString(1, registro);

			ResultSet rs = ps.executeQuery();

			if (rs.next()) {
				int idRfc = rs.getInt("id_rfc");
				String denominacion = rs.getString("denominacion");

				RfcTO rfcTO = new RfcTO(idRfc, registro, denominacion);
				ps.close();
				conexion.close();
				return rfcTO;
			}
			ps.close();
			conexion.close();
			return null;

		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}

	@Override
	public boolean insertRfc(RfcTO rfcTO) {
		try {
			Connection conexion = FabricaDeDAOsMySQL.crearConexion();

			PreparedStatement ps = conexion.prepareStatement(INSERT);

			ps.setString(1, rfcTO.getRegistro());
			ps.setString(2, rfcTO.getDenominacion());

			ps.executeUpdate();

			ps.close();
			conexion.close();

			return true;
		} catch (Exception e) {
			e.printStackTrace();
			return false;
		}
	}

	@Override
	public ArrayList<RfcTO> selectRfc() {
		try {
			Connection conexion = FabricaDeDAOsMySQL.crearConexion();

			PreparedStatement ps = conexion.prepareStatement(SELECT);

			ResultSet rs = ps.executeQuery();

			ArrayList<RfcTO> coleccion = null;

			while (rs.next()) {
				if (coleccion == null)
					coleccion = new ArrayList<RfcTO>();

				int idRfc = rs.getInt("id_rfc");
				String registro = rs.getString("registro");
				String denominacion = rs.getString("denominacion");

				RfcTO rfcTO = new RfcTO(idRfc, registro, denominacion);
				coleccion.add(rfcTO);
			}
			ps.close();
			conexion.close();

			return coleccion;

		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}

	@Override
	public short updateRfc(RfcTO rfcTO) {
		try {
			Connection conexion = FabricaDeDAOsMySQL.crearConexion();

			PreparedStatement ps = conexion.prepareStatement(UPDATE);

			ps.setString(1, rfcTO.getRegistro());
			ps.setString(2, rfcTO.getDenominacion());
			ps.setInt(3, rfcTO.getIdRfc());

			ps.executeUpdate();

			if (ps.getUpdateCount() == 0) {
				ps.close();
				conexion.close();
				return 0;
			} else {
				ps.close();
				conexion.close();
				return 1;
			}
		} catch (Exception e) {
			e.printStackTrace();
			return (short)-1;
		}
	}

}
